package top.kaoshanji.p1ch03a;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 代码清单test-p1ch03a-75页a：使用profile进行测试
 * @author kaoshanji
 * ^_^
 * create time 2025/4/23 9:26
 */
public class DataSourceConfigTest {

    /**
     * 测试环境
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = DataSourceConfig.class)
    @ActiveProfiles("dev")
    public static class DevDataSourceTest {

        @Autowired
        private DataSource dataSource;

        @Test
        public void shouldBeEmbeddedDatasource() {

            Assert.assertNotNull(dataSource);

            JdbcTemplate jdbc = new JdbcTemplate(dataSource);
            List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet resultSet, int i) throws SQLException {
                    return resultSet.getLong("id") + ":" + resultSet.getString("name");
                }
            });

            Assert.assertEquals(1, results.size());
            Assert.assertEquals("1:A", results.get(0));
        }

    }


    /**
     * 生产环境
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = DataSourceConfig.class)
    @ActiveProfiles("prod")
    public static class ProductionDataSourceTest {

    }



}
